package com.hgf.helper.mybatisplusdemo.controller;
import java.util.Date;
import java.time.LocalDateTime;

import com.hgf.helper.mybatisplusdemo.db.entity.CardInfo;
import com.hgf.helper.mybatisplusdemo.db.entity.RewardInfo;
import com.hgf.helper.mybatisplusdemo.db.entity.ScoreInfo;

import com.hgf.helper.mybatisplusdemo.db.entity.Student;
import com.hgf.helper.mybatisplusdemo.db.service.CardInfoServiceImpl;
import com.hgf.helper.mybatisplusdemo.db.service.RewardInfoServiceImpl;
import com.hgf.helper.mybatisplusdemo.db.service.ScoreInfoServiceImpl;
import com.hgf.helper.mybatisplusdemo.db.service.StudentServiceImpl;
import com.hgf.helper.mybatisplusdemo.utils.RandUtil;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

/**
 * mock 数据接口
 */
@RequestMapping("mock")
@RestController
public class MockController {

    @Autowired
    StudentServiceImpl studentService;

    @Autowired
    ScoreInfoServiceImpl scoreInfoService;

    @Autowired
    RewardInfoServiceImpl rewardInfoService;

    @Autowired
    CardInfoServiceImpl cardInfoService;


    /***
     * 生成指定数量的student数据
     * @param num                       数量
     * @param generateScoreAndReward    是否级联生成score和reward
     */
    @GetMapping("generateStu")
    public boolean generateStu(int num,@RequestParam("caseGenerate") boolean generateScoreAndReward) {

        List<Student> students = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            Student student = new Student();
            student.setName(RandUtil.getChineseName());
            student.setBirthday(LocalDateTime.now());
            student.setSex(RandomUtils.nextInt(1, 2));
            student.setNo(System.currentTimeMillis()+"");
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            students.add(student);
        }

        boolean b = studentService.saveBatch(students);
        if (!b) {
            return false;
        }

        if (generateScoreAndReward) {
            for (Student student : students) {
                generateScore(1, student.getNo());
                generateReward(RandomUtils.nextInt(1, 5), student.getNo());
                generateCard(1, student.getNo());
            }

        }


        return true;
    }

    /***
     * 生成指定数量的score数据
     * @param num                       数量
     * @param stuNo                     学生信息编号
     */
    @GetMapping("generateScore")
    public boolean generateScore(int num, String stuNo) {
        List<ScoreInfo> scoreInfos = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            ScoreInfo scoreInfo = new ScoreInfo();
            scoreInfo.setStuNo(stuNo);
            scoreInfo.setScore(RandomUtils.nextInt(1,90));
            scoreInfo.setStuScore(RandomUtils.nextInt(1,80));
            scoreInfo.setStuTime(RandomUtils.nextInt(1,5));
            scoreInfo.setSubjectNo(RandomUtils.nextInt(100, 10000) + "");
            scoreInfos.add(scoreInfo);
        }

        return scoreInfoService.saveBatch(scoreInfos);
    }


    /***
     * 生成指定数量的card数据
     * @param num                       数量
     * @param stuNo                     学生信息编号
     */
    @GetMapping("generateCard")
    public boolean generateCard(int num, String stuNo) {
        List<CardInfo> cardInfos = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            CardInfo cardInfo = new CardInfo();
            cardInfo.setNo(System.currentTimeMillis() + "");
            cardInfo.setStuNo(stuNo);


            cardInfos.add(cardInfo);
        }

        return cardInfoService.saveBatch(cardInfos);
    }


    /***
     * 生成指定数量的reward数据
     * @param num                       数量
     * @param stuNo                     学生信息编号
     */
    @GetMapping("generateReward")
    public boolean generateReward(int num, String stuNo) {
        List<RewardInfo> rewardInfos = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            RewardInfo rewardInfo = new RewardInfo();
            rewardInfo.setStuNo(stuNo);
            rewardInfo.setContent(RandUtil.getRandomStr(100));
            rewardInfos.add(rewardInfo);
        }

        return rewardInfoService.saveBatch(rewardInfos);
    }



}
